package by.bsu.scs.presentation.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import by.bsu.scs.db.dao.TaskAttachmentHome;
import by.bsu.scs.model.TaskAttachment;
import by.bsu.scs.presentation.form.TaskForm;
import by.bsu.scs.util.form.FileManager;
import by.bsu.scs.util.form.StudentTaskFormFiller;
import by.bsu.scs.util.manager.SessionManager;

/**
 * @author Svetlana Voiteh
 * @version 1.0
 * 
 * All actions connected with tasks for students.
 */

public class StudentTaskAction extends DispatchAction {

	private static final String INDEX = "index";
	private static final String STATE = "state";

	private static final String LIST = "list";
	private static final String VIEW = "view";
	private static final String SOLUTION = "solution";
	private static final String UNCHECKED = "unchecked";

	/**
	 * Download file
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward download(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;
		taskForm.setStudentTask(taskForm.getStudentTaskList().get(
				Integer.parseInt(request.getParameter(INDEX))));
		File file = taskForm.getStudentTask().getTaskAttach();
		FileManager fileDownload = new FileManager();
		fileDownload.download(file, response);
		
		return null;
	}
	
	/**
	 * Create list of all tasks for student
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;
		StudentTaskFormFiller formFiller = new StudentTaskFormFiller(taskForm,
				SessionManager.getInstance().getUser(request));
		taskForm.setDisciplineId(0);
		formFiller.fillElementsForAllTasks();
		return mapping.findForward(LIST);
	}

	/**
	 * Отвечает за фильтрацию заданий по учебной дисциплине
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;
		StudentTaskFormFiller formFiller = new StudentTaskFormFiller(taskForm,
				SessionManager.getInstance().getUser(request));
		String state = request.getParameter(STATE);
		String result = null;
		if(LIST.equalsIgnoreCase(state)){
			formFiller.fillElementsForAllTasks();
			result = LIST;
		}
		if(UNCHECKED.equalsIgnoreCase(state)){
			formFiller.fillElementsForUncheckedTasks();
			result = UNCHECKED;
		}
		return mapping.findForward(result);
	}

	/**
	 * Заполняет элементы формы для отправки решения по выбранному заданию
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;
		taskForm.setStudentTask(taskForm.getStudentTaskList().get(
				Integer.parseInt(request.getParameter(INDEX))));
//		StudentTaskFormFiller formFiller = new StudentTaskFormFiller(taskForm,
//				SessionManager.getInstance().getUser(request));
//		formFiller.createPolicyDescription();
		return mapping.findForward(VIEW);
	}

	/**
	 * Send file-solution 
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward solution(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		TaskForm taskForm = (TaskForm) form;
		StudentTaskFormFiller formFiller = new StudentTaskFormFiller(taskForm,
				SessionManager.getInstance().getUser(request));
		
		TaskAttachment solution = formFiller.createSolution(this);
		new TaskAttachmentHome().persist(solution);		
		return mapping.findForward(SOLUTION);
	}

	/**
	 * Create list of unchecked tasks for student
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward unchecked(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		TaskForm taskForm = (TaskForm) form;
		StudentTaskFormFiller formFiller = new StudentTaskFormFiller(taskForm,
				SessionManager.getInstance().getUser(request));
		taskForm.setDisciplineId(0);
		formFiller.fillElementsForUncheckedTasks();
		return mapping.findForward(UNCHECKED);
	}
	
	
}
